package cn.lingyangwl.agile.food.core.controller.admin;

import cn.lingyangwl.agile.model.constants.*;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.food.model.entity.GoodsAttribute;
import cn.lingyangwl.agile.food.core.service.GoodsAttributeService;
import cn.lingyangwl.agile.tenant.core.Tenant;
import cn.lingyangwl.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 商品属性
 * 
 * @author shenguangyang
 */
@Api(tags = "商品属性")
@RestController
@RequestMapping("/goodsAttr")
public class GoodsAttributeController {
    @Resource
    private GoodsAttributeService goodsAttributeService;

    @ApiOperation(value = "查询商品属性")
    @GetMapping("/list")
    @RamResource(action = "goods:attribute:query", name = "查询商品属性", type = ActionType.READ)
    @Tenant(level = DataIsolateLevel.MAIN)
    public Resp<List<GoodsAttribute>> listAttribute() {
        return Resp.ok(goodsAttributeService.list());
    }
  
    @ApiOperation(value = "保存商品属性")
    @PostMapping("/save")
    @RamResource(action = "goods:attribute:save", name = "保存商品属性", type = ActionType.WRITE)
    public Resp<?> saveGoodsAttribute(@Validated @RequestBody GoodsAttribute req) {
        return Resp.ok(goodsAttributeService.saveOrUpdateAttribute(req));
    }
  
    @ApiOperation(value = "更新商品属性")
    @PostMapping("/update")
    @RamResource(action = "goods:attribute:update", name = "更新商品属性", type = ActionType.WRITE)
    public Resp<?> updateGoodsAttribute(@Validated @RequestBody GoodsAttribute req) {
        return Resp.ok(goodsAttributeService.saveOrUpdateAttribute(req));
    }
  
    @ApiOperation(value = "批量删除商品属性")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "goods:attribute:delete", name = "删除商品属性", type = ActionType.WRITE)
    public Resp<?> batchDeleteGoodsAttribute(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(goodsAttributeService.batchDeleteAttribute(ids));
    }
  
    @ApiOperation(value = "删除商品属性")
    @DeleteMapping("/delete")
    @RamResource(action = "goods:attribute:delete", name = "删除商品属性", type = ActionType.WRITE)
    public Resp<?> deleteGoodsAttribute(@RequestParam(value = "id") Long id) {
        return Resp.ok(goodsAttributeService.batchDeleteAttribute(Collections.singletonList(id)));
    }
}
